package com.zhuyuan.controller;

import com.zhuyuan.Entity.Navigation;
import com.zhuyuan.common.R;
import com.zhuyuan.repository.NavigationRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/Navigation")
@Api(tags = "导航网址相关接口")
public class NavigationController {
    @Autowired
    private NavigationRepository navigationRepository;

    @GetMapping("/fillAll")
    @ApiOperation("查询所有网址")
    public R<List<Navigation>> fillAll(){
        List<Navigation> list = navigationRepository.findActiveNavigations();
        return R.success(list);
    }

    @PostMapping("/handleSubmit")
    @ApiOperation("新增网址")
    public R<String> submitForm(@RequestBody Navigation navigation){
        log.info("新增/修改：{}",navigation);
        Navigation navigation1 = new Navigation();
        BeanUtils.copyProperties(navigation,navigation1);
        navigation1.setStatus(1);
        navigationRepository.save(navigation1);
        return R.success("新增成功");
    }

    @Transactional
    @PostMapping("/confirmDelete")
    @ApiOperation("删除网址")
    public R<String> delete(@RequestBody Navigation navigation){
        log.info("删除：{}",navigation);
        navigationRepository.isdelete(navigation.getTitle(),navigation.getUrl());
        return R.success("删除成功");
    }
}
